package com.ls.lserp.modules.custom.service;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CacheUtils;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.modules.common.entity.UploadFile;
import com.ls.lserp.modules.custom.dao.CustomDao;
import com.ls.lserp.modules.custom.utils.DesignOptionUtils;
import com.ls.lserp.modules.sys.dao.DictDao;
import com.ls.lserp.modules.setting.dao.ProcessDao;
import com.ls.lserp.modules.sys.entity.Dict;
import  com.ls.lserp.modules.order.entity.DesignOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 款式设计
 * @author zq
 * @version 2016-10-25
 */
@Service
@Transactional(readOnly = true)
public class CustomService extends CrudService<DictDao, Dict> {
	@Autowired
	private CustomDao customDao;
	
	@Autowired
	private ProcessDao  uploadFileDao;
	
    //获取父项信息
    public DesignOption getDesignOption(Long id){
        
        DesignOption designOption=customDao.get(id);;
        
        return designOption;
    }
	
	/**
	 * 查询父项
	 * @param page
	 * @param user
	 * @return
	 */
    public Page<DesignOption> findDesignOption(Page<DesignOption> page, DesignOption designOption) {
        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
        //designOption.getSqlMap().put("dsf", dataScopeFilter(user.getCurrentUser(), "o", "a"));
        // 设置分页参数
        designOption.setPage(page);
        // 执行分页查询
        page.setList(customDao.findList(designOption));
        return page;
    }

    /**
     * 查询子项
     * @param page
     * @param user
     * @return
     */
    public List<DesignOption> findSubDesignOption(DesignOption designOption) {
        // 执行查询
        return customDao.findSubList(designOption);
    }
	
    /**
     * 根据itemCD查询父项是否已经存在
     * @param designOption
     * @return
     */
    public  DesignOption searchDesginOptionInfoByItemCD(String itemCD) {
        
        return   customDao.searchDesginOptionInfoByItemCD(itemCD);
        
    }
    
    
    /**
     * 根据itemvalue查询父项是否已经存在
     * @param designOption
     * @return
     */
    public  DesignOption searchDesginOptionInfoByItemValue(String itemValue) {
        
        return   customDao.searchDesginOptionInfoByItemValue(itemValue);
        
    }
    
    /**
     * 查询所有数据包括已删除
     * @param designOption
     * @return
     */
    
    public List<DesignOption> findAllListExport( DesignOption designOption) {
        // 执行查询
        return customDao.findAllListExport(designOption);
    }
    
    /**
     * 保存款式
     * @param designOption
     */
    @Transactional(readOnly=false)
	public void saveDesignOption(DesignOption designOption) {
       
        //新建
        if(designOption.getId()==null){
            
            designOption.preInsert();
            customDao.insert(designOption);
        }else{
            //修改
            designOption.preUpdate();
            customDao.update(designOption);
        }
        //如果子项不为空
        if(!CollectionsUtils.isEmpty(designOption.getDesignOptionSubList())){
            for(int i=0; i<designOption.getDesignOptionSubList().size();i++) {
                //获取图片信息
                UploadFile uploadFile = designOption.getDesignOptionSubList().get(i).getUploadFile();
                //保存图片
                if (uploadFile != null) {
                    //图片存在的情况
                    if (!CollectionsUtils.isEmpty(uploadFileDao.searchFileId(uploadFile))) {
                        uploadFile.preUpdate();
                        uploadFile.setId(uploadFile.getFileId());
                        uploadFileDao.updateFile(uploadFile);
                    } else {
                        //子项取得图片Id
                        designOption.getDesignOptionSubList().get(i).setFileID(uploadFile.getId());
                        uploadFile.preInsert();
                        uploadFileDao.addUploadFile(uploadFile);
                    }
                }
                designOption.getDesignOptionSubList().get(i).setUploadFile(uploadFile);
                if (designOption.getDesignOptionSubList().get(i).getId() == null) {
                    designOption.getDesignOptionSubList().get(i).preInsert();
                    //子项顺序
                    long j = 1L;
                    //顺序
                    designOption.getDesignOptionSubList().get(i).setItemSort(j++);
                    //子项款式分类
                    designOption.getDesignOptionSubList().get(i).setStyleCategoryCD(designOption.getStyleCategoryCD());
                    //设置子项Code
                    designOption.getDesignOptionSubList().get(i).setItemCD(designOption.getItemValue());
                    customDao.insert(designOption.getDesignOptionSubList().get(i));
                } else {
                    designOption.getDesignOptionSubList().get(i).preUpdate();
                    designOption.getDesignOptionSubList().get(i).setVersion(designOption.getDesignOptionSubList().get(i).getVersion() + 1);
                    customDao.updateSubItem(designOption.getDesignOptionSubList().get(i));
                }
            }

	       //清除缓存
	       CacheUtils.remove(DesignOptionUtils.CACHE_DESIGNOPTION_MAP);
	       CacheUtils.remove(DesignOptionUtils.CACHE_DESIGNOPTION_VALUE_MAP);
	   }
    }
    /**
     * 删除款式（逻辑删除）
     * @param designOption
     * @return
     */
	@Transactional(readOnly=false)
	public void deleteDesignOption(DesignOption designOption) {
		//删除子项
	    customDao.logicDeleteSub(designOption);
	    customDao.delete(designOption);
	    CacheUtils.remove(DesignOptionUtils.CACHE_DESIGNOPTION_MAP);
        CacheUtils.remove(DesignOptionUtils.CACHE_DESIGNOPTION_VALUE_MAP);
	}
	

    /**
     * 导入excel
     * @param list
     * @return
     */
    @Transactional(readOnly = false)
    public Integer saveCustomImport(List<DesignOption> list) {
        
    	customDao.deleteAll(new DesignOption());
        Integer i=0;
        for (DesignOption designOption : list) {
        	designOption.preInsert();
            // 新建版本号赋值1
        	designOption.setVersion(1L);
            customDao.insert(designOption);
            i++;
        }
        return i;
    }
	
    public CustomDao getCustomDao() {
        return customDao;
    }

    public void setCustomDao(CustomDao customDao) {
        this.customDao = customDao;
    }

    @Transactional(readOnly=false)
	public void updateCustomDao(DesignOption designOption)
    {
        designOption.getId();
        // 更新数据
        if (designOption.getId() != null){
            designOption.preUpdate();
            customDao.update(designOption);
        }

    }
    @Transactional(readOnly=false)
    public void updateSubItem(DesignOption designOption)
    {
        designOption.getId();
        // 更新数据
        if (designOption.getId() != null){
            designOption.preUpdate();
            customDao.updateSubItem(designOption);
        }

    }

	
}
